#include <iostream>
#include <string>
#include <vector>
#include "json.hpp"
using json = nlohmann::json;

//序列化内置类型
std::string fun1()
{
    json js;
    js["sendto1"] = "zhang san";
    js["sendto2"] = "li si";
    js["sendto3"] = "wang wu";
    js["sendto4"] = "zhao liu";

    std::string sendtoMeg = js.dump();

    return sendtoMeg;
}

//序列化自定义类型
std::string fun2()
{
    json js;
    js["Value1"] = {1,2,3,4,5};

    std::map<int,std::string> account;
    account.insert(std::make_pair(1,"zhang san"));
    account.insert(std::make_pair(2,"li si"));

    js["Value2"] = account;

    std::string sendtoMeg = js.dump();

    return sendtoMeg;
}

//反序列化  将json string序列化字符串 反序列化为 json对象
void fun3(std::string& in, json* out)
{
    *out = json::parse(in);
}


int main()
{
    std::string Meg1 = fun1();
    std::cout << Meg1 << std::endl;

    json recvce1 = nullptr;
    fun3(Meg1,&recvce1);

    std::cout << recvce1["sendto1"] << std::endl;
    std::cout << recvce1["sendto2"] << std::endl;
    std::cout << recvce1["sendto3"] << std::endl;
    std::cout << recvce1["sendto4"] << std::endl;


    std::string Meg2 = fun2();
    std::cout << Meg2 << std::endl;

    json recvce2 = nullptr;
    fun3(Meg2,&recvce2);

    auto megValue1 = recvce2["Value1"].get<std::vector<int>>();
    auto megValue2 = recvce2["Value2"].get<std::map<int,std::string>>();

    for(auto & e : megValue1)
    std::cout << e << " ";
    std::cout << std::endl;

    for(auto& e : megValue2)
    std::cout << e.second << " ";
    std::cout << std::endl;


    return 0;
}




